﻿var floor = -1;
var enable = false;

function loadFinish(){
	enable = true;
}

function addFood(url, id, name, price) {
	
	//alert("ss");
	if (!enable)
		return;
	
	var left=document.getElementById("left_pid_"+id);
	//alert(left.value);
	if(left.value<=0)
		{alert("Not enough product!");return;}
	else
		{left.value=parseInt(left.value)-1;}
	
	var tr = foodList.insertRow(foodList.rows.length);
	var td;
	for ( var i = 0; i < foodList.rows[0].cells.length; i++) {
		td = tr.insertCell();
		switch (i) {
		case 0:
			if (navigator.appName.indexOf("Microsoft") != -1)
			{td.innerHTML = "<p>" + id + "</p>";}
                        else{
			td.innerHTML = "<p>" + price + "</p>";
				} 
			break;
		case 1:
			td.innerHTML = "<p>" + name + "</p>";
			break;
		case 2:
			if (navigator.appName.indexOf("Microsoft") != -1)
			{td.innerHTML = "<p>" + price + "</p>";}
                        else{
			td.innerHTML = "<p>" + id + "</p>";
				} 
			break;
		}
	}

	var a = thisMovie("swfm").addFood(url, id);
}

function addFloor(url, id, name, price) {
	
	if (!enable)
		return;
	
	removeFloor();
	var left=document.getElementById("left_pid_"+id);
	//alert(left.value);
	if(left.value==0)
		{alert("Not enough product!");return;}
	else
		{left.value=parseInt(left.value)-1;}
	
	
	floor = id;
	var tr = foodList.insertRow(foodList.rows.length);
	var td;
	for ( var i = 0; i < foodList.rows[0].cells.length; i++) {
		td = tr.insertCell();
		switch (i) {
		case 0:
			if (navigator.appName.indexOf("Microsoft") != -1)
			{td.innerHTML = "<p>" + id + "</p>";}
                        else{
			td.innerHTML = "<p>" + price + "</p>";
				} 
			break;
		case 1:
			td.innerHTML = "<p>" + name + "</p>";
			break;
		case 2:
			if (navigator.appName.indexOf("Microsoft") != -1)
			{td.innerHTML = "<p>" + price + "</p>";}
                        else{
			td.innerHTML = "<p>" + id + "</p>";
				} 
			break;
		}
	}

	var a = thisMovie("swfm").addFloor(url, id);
}

function removeFood(id) {
	var tbl = document.getElementById("foodList");
	var length = tbl.rows.length;
	var name = "<p>" + id + "</p>";
	for ( var i = 0; i < length; i++) {
		var chk = tbl.rows[i].cells[0].innerHTML;
		if (chk == name) {
			tbl.deleteRow(i);
			var left=document.getElementById("left_pid_"+id);
			left.value=parseInt(left.value)+1;
			break;
		}
	}
}

function removeFloor() {
	if (floor != -1) {
		var tbl = document.getElementById("foodList");
		var length = tbl.rows.length;
		var name = "<p>" + floor + "</p>";
		for ( var i = 0; i < length; i++) {
			var chk = tbl.rows[i].cells[0].innerHTML;
			if (chk == name) {
				tbl.deleteRow(i);
				var left=document.getElementById("left_pid_"+floor);
				left.value=parseInt(left.value)+1;
				break;
			}
		}
	}
}

// thisMoivie 参数：是swf文件 的id
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
function put_in_cart()
{
	
	var ids="";
	
	var tbl = document.getElementById("foodList");
	var length = tbl.rows.length;
	
	if(parseInt(length)==1)
		{return;}
	
	for ( var i = 1; i < length; i++) {
		var str=tbl.rows[i].cells[0].innerHTML;
		//alert(str.length);
		ids=ids+(str.substring(3,str.length-4))+",";		
	}
	ids=ids.substring(0,ids.length-1);
	//alert(ids);
	var url="cart.php?pids="+ids;
	//alert(url);
	window.location.href=url;
}